﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using BEGDC.CC.BL.Common;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.Entity;
using BEGDC.Utility.Castle;

namespace BEGDC.CC.Web.Workflow
{
    /// <summary>
    /// Summary description for ShowItemForm.
    /// </summary>
    public class SaveAdvancedSearch : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        private int _SearchID
        {
            get
            {
                if (Session["_SearchID"] == null) return 0;

                return Convert.ToInt32(Session["_SearchID"]);
            }
            set { Session["_SearchID"] = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        private string _SearchType
        {
            get
            {
                if (Session["SearchType"] == null) return "CR";

                return (string)Session["SearchType"];
            }
            set { Session["SearchType"] = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        private string _SearchSQL_Fields
        {
            get
            {
                if (Session["_SearchSQL_Fields"] == null) return string.Empty;

                return (string)Session["_SearchSQL_Fields"];
            }
            set { Session["_SearchSQL_Fields"] = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        private string _SearchSQL_Condition
        {
            get
            {
                if (Session["_SearchSQL_Condition"] == null) return string.Empty;

                return (string)Session["_SearchSQL_Condition"];
            }
            set { Session["_SearchSQL_Condition"] = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        private string _SearchSQL
        {
            get
            {
                if (Session["SearchSQL"] == null) return string.Empty;

                return (string)Session["SearchSQL"];
            }
            set { Session["SearchSQL"] = value; }
        }


        private DataTable _SearchResult
        {
            get
            {
                if (Session["ChangeSearchResult"] == null) return null;

                else return (DataTable)Session["ChangeSearchResult"];
            }
            set
            {
                Session["ChangeSearchResult"] = value;
            }
        }

        private System.Collections.Hashtable _SearchFields
        {
            get
            {
                if (ViewState["SearchFields"] == null) return null;

                else return (System.Collections.Hashtable)ViewState["SearchFields"];
            }
            set
            {
                ViewState["SearchFields"] = value;
            }
        }

        protected System.Web.UI.WebControls.Label lblTitle;
        protected System.Web.UI.WebControls.Label lbName;
        protected System.Web.UI.WebControls.Button btnSave;
        protected System.Web.UI.WebControls.Label lbRename;
        protected System.Web.UI.WebControls.Label lbDesc;
        protected System.Web.UI.WebControls.DropDownList ddlSearchs;
        protected System.Web.UI.WebControls.TextBox txtRename;
        protected System.Web.UI.WebControls.TextBox txtDescription;
        protected System.Web.UI.WebControls.Button btnClose;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            if (!Page.IsPostBack)
            {

                //InitializeGlobalization();

                this.btnSave.Attributes.Add("onclick", "return confirm('Are you sure you want to save the search?') ;");

                //string id = Request.QueryString["id"];
                BindDataItem(Convert.ToInt32(this._SearchID));


            }
        }

        private void BindDataItem(int id)
        {
            BEGDC.CC.BL.AdvancedSearch.IAdvancedSearch iquery = AppContainer.Instance[ServiceConst.SearchService] as BEGDC.CC.BL.AdvancedSearch.IAdvancedSearch;

            System.Data.DataTable dtSearchs = iquery.QueryMySearchList(((CCIdentity)HttpContext.Current.User.Identity).UserInfo.Id, this._SearchType);

            //if (dtSearchs.Rows.Count == 0)
            this.ddlSearchs.Items.Add(new System.Web.UI.WebControls.ListItem("New Search", string.Empty));
            foreach (System.Data.DataRow dr in dtSearchs.Rows)
            {
                this.ddlSearchs.Items.Add(new System.Web.UI.WebControls.ListItem(dr["SearchName"].ToString(), dr["SearchID"].ToString()));
            }

            if (id > 0)
            {
                foreach (System.Data.DataRow dr in dtSearchs.Rows)
                {
                    if (dr["SearchID"].ToString() == id.ToString())
                    {
                        this.ddlSearchs.SelectedValue = dr["SearchID"].ToString();
                        this.txtDescription.Text = dr["Description"].ToString();
                    }
                }
            }
        }

        //        private void InitializeGlobalization()
        //        {
        ////			this.lblTitle.Text = "Save Advanced Search";
        //            this.btnSave.ImageUrl = "../images/bbw_Save.gif";
        //            this.btnClose.ImageUrl = "../images/bbw_Close.gif";
        //        }
        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        void btnSave_Click(object sender, EventArgs e)
        {
            BEGDC.CC.BL.AdvancedSearch.IAdvancedSearch iquery = AppContainer.Instance[ServiceConst.SearchService] as BEGDC.CC.BL.AdvancedSearch.IAdvancedSearch;
            BEGDC.CC.Entity.AdvancedSearch search = new BEGDC.CC.Entity.AdvancedSearch();

            //			if (this._SearchID > 0)
            //			{
            //				search.Id = _SearchID;
            //			}
            if (this.ddlSearchs.SelectedValue != string.Empty)
            {
                // Save 
                search.Id = Convert.ToInt32(this.ddlSearchs.SelectedValue);
                if (this.txtRename.Text != string.Empty)
                    search.SearchName = this.txtRename.Text;
                else
                {
                    search.SearchName = this.ddlSearchs.SelectedItem.Text;
                }
                search.Description = this.txtDescription.Text;
            }

            else
            {
                // Save as
                if (this.txtRename.Text == string.Empty)
                {
                    Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.SM013 + "');</script>");
                    return;
                }
                search.SearchName = this.txtRename.Text;
                search.Description = this.txtDescription.Text;
            }

            Employee employee = new Employee();
            CCIdentity id = this.User.Identity as CCIdentity;
            Employee emp = new Employee();
            emp.Id = id.UserInfo.Id;
            search.Employee = emp;
            search.SearchCondition = this._SearchSQL_Condition;
            search.SearchSQL = this._SearchSQL;
            search.SearchField = this._SearchSQL_Fields;

            IUtility utility = BEGDC.Utility.Castle.AppContainer.Instance[ServiceConst.UtilityService] as IUtility;


            search.SearchType = _SearchType;

            int result = 0;
            if (search.Id > 0)
                result = iquery.SaveAdvancedSearch(search);
            else
                result = iquery.AddAdvancedSearch(search);
            if (result == -1)
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.SM010 + "');</script>");
            else if (result == 0)
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");
            else
            {

                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR001 + "');returnValue='Item';window.close();</script>");
                BEGDC.CC.Entity.OperationLog log = new BEGDC.CC.Entity.OperationLog();
                log.Module = "Advanced Search";
                emp = new Employee();
                emp.Id = id.UserInfo.Id;
                log.Employee = emp;
                log.Time = System.DateTime.Now;
                log.Description = "Update data advance search. Searchname:" + search.SearchName;
                utility.SaveOperationLog(log);
            }

        }

        void btnClose_Click(object sender, EventArgs e)
        {
            Page.RegisterClientScriptBlock("", "<script>returnValue='';window.close();</script>");
        }
    }
}
